//
//  EditViewController.m
//  SimpleNote
//
//  Created by mac on 2017/11/21.
//  Copyright © 2017年 上海LOL信息技术服务有限公司. All rights reserved.
//

#import "EditViewController.h"
#import <IQKeyboardManager.h>
#import "UIBarButtonItem+SRExtension.h"

@interface EditViewController ()
@property (nonatomic,strong) UIScrollView *scrollview;
@property (nonatomic,strong) UITextField *titleTextField;
@property (nonatomic,strong) UITextView *contentTextView;

@end

@implementation EditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)complectionAction{
    
    [self.view endEditing:YES];
    
    if ([self.titleTextField.text isEqualToString:@""] && [self.contentTextView.text isEqualToString:@""]) {
        [MBProgressHUD showTipMessageInView:NSLocalizedString(@"Please enter the content", nil)];
        return ;
    }

    if ([self.titleTextField.text isEqualToString:@""]) {
        self.titleTextField.text = NSLocalizedString(@"No title content", nil);
    }

    if (self.recordModel) {
        
        AVObject *todoFolder = [[AVObject alloc] initWithClassName:@"SaveContent"];// 构建对象
        [todoFolder setObject:self.contentTextView.text forKey:@"content"];// setting名称
        [SVProgressHUD show];
        [todoFolder saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
            [SVProgressHUD dismiss];
            // 修改
            self.recordModel.title = self.titleTextField.text;
            self.recordModel.content = self.contentTextView.text;
            self.recordModel.time = [SDHelper getCurrentTimes];
            [[DataOperationLayer shareInstance]updateBookRecord:self.recordModel];
            [MBProgressHUD showTipMessageInView:@"更新成功"];
        }];

    }else{
        
        AVObject *todoFolder = [[AVObject alloc] initWithClassName:@"SaveContent"];// 构建对象
        [todoFolder setObject:self.contentTextView.text forKey:@"content"];// setting名称
        // 保存到云端
        [SVProgressHUD show];
        [todoFolder saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
            [SVProgressHUD dismiss];
            // 增加
            BookRecord *record = [[BookRecord alloc]init];
            record.title = self.titleTextField.text;
            record.content = self.contentTextView.text;
            record.time = [SDHelper getCurrentTimes];
            [[DataOperationLayer shareInstance]insertBookRecord:record ResultBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    [MBProgressHUD showTipMessageInView:@"保存成功"];
                }else{
                    [MBProgressHUD showTipMessageInWindow:@"保存失败"];
                }
            }];
        }];
    }
}

#pragma mark ---

-(void)setupUI{
    
    if (@available(iOS 11.0, *)) {
        self.scrollview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    self.title = NSLocalizedString(@"Diary", nil);
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTitle:NSLocalizedString(@"finish", nil) addTarget:self action:@selector(complectionAction)];
    
    [self.view addSubview:self.scrollview];
    [self.scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(NavigationBarHeight);
    }];
    
    // 这是一个中间层  http://www.cocoachina.com/ios/20141219/10702.html
    UIView *contentView = [[UIView alloc]init];
    [self.scrollview addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollview);
        make.width.equalTo(self.scrollview);
        make.height.mas_equalTo(kScreen.height);
    }];
    
    
    [contentView addSubview:self.titleTextField];
    [self.titleTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView).offset(12*ScaleX);
        make.right.equalTo(contentView).offset(-12*ScaleX);
        make.top.equalTo(contentView).offset(12*ScaleX);
        make.height.mas_equalTo(44*ScaleX);
    }];

    [contentView addSubview:self.contentTextView];
    [self.contentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView).offset(12*ScaleX);
        make.right.equalTo(contentView).offset(-12*ScaleX);
        make.top.equalTo(self.titleTextField.mas_bottom).offset(12*ScaleX);
        make.bottom.equalTo(contentView).offset(-12*ScaleX);;
    }];

}

#pragma mark --- set
-(void)setRecordModel:(BookRecord *)recordModel{
    _recordModel = recordModel;
    
    self.titleTextField.text = recordModel.title;
    self.contentTextView.text = recordModel.content;
}

#pragma mark --- get
-(UITextField *)titleTextField{
    if (_titleTextField == nil) {
        _titleTextField = [[UITextField alloc]init];
        _titleTextField.placeholder = NSLocalizedString(@"No title content", nil);
        _titleTextField.font = [UIFont boldSystemFontOfSize:24*ScaleX];
        _titleTextField.backgroundColor = [UIColor clearColor];
    }
    return _titleTextField;
}

-(UITextView *)contentTextView{
    if (_contentTextView == nil) {
        _contentTextView = [[UITextView alloc]init];
        
        // _placeholderLabel
        UILabel *placeHolderLabel = [[UILabel alloc] init];
        placeHolderLabel.text = NSLocalizedString(@"Please enter the content", nil);
        placeHolderLabel.numberOfLines = 0;
        placeHolderLabel.textColor = [UIColor lightGrayColor];
        [placeHolderLabel sizeToFit];
        [_contentTextView addSubview:placeHolderLabel];
        
        // same font
        _contentTextView.font = [UIFont systemFontOfSize:20*ScaleX];
        placeHolderLabel.font = [UIFont systemFontOfSize:20*ScaleX];
        
        [_contentTextView setValue:placeHolderLabel forKey:@"_placeholderLabel"];

    }
    return _contentTextView;
}

-(UIScrollView *)scrollview{
    if (_scrollview == nil) {
        _scrollview = [[UIScrollView alloc]init];
    }
    return _scrollview;
}


@end








